const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    questions:null,
    customary:null,
    sortOptions: [
      { text: '默认排序', value: 0 },
      { text: '难度从高到低', value: 1 },
      { text: '难度从低到高', value: 2 },
    ],
    sort: 0,
    type:'',
  },
  sortChange(e){
    console.log(e.detail);
    this.setData({
      sort:e.detail
    })
    const db = wx.cloud.database()
    switch (e.detail) {
      case 0:
        db.collection('questions').orderBy('order', 'asc').where({
          type:this.data.type
        })
        .get()
        .then((res)=>{
          console.log(res);
          this.setData({
            questions:res.data
          })
        })
        .catch(console.error)
        break;
        // console.log(this.data.questions);
      case 1:
        db.collection('questions').orderBy('difficulty', 'desc')
        .where({
          type:this.data.type
        })
        .get()
        .then((res)=>{
          console.log(res);
          this.setData({
            questions:res.data
          })
        })
        .catch(console.error)
        break;
      case 2:
        console.log(this.data.type);
        db.collection('questions').orderBy('difficulty', 'asc')
        .where({
          type:this.data.type
        })
        .get()
        .then((res)=>{
          console.log(res);
          this.setData({
            questions:res.data
          })
        })
        .catch(console.error)
      default:
        break;
    }
  },
  
  goToDetail(e){
    // console.log(this.data.sort);
    wx.navigateTo({
      url: `/pages/question_detail/question_detail?type=${this.data.type}&index=${e.currentTarget.dataset.index}&order=${this.data.sort}`,
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    this.setData({
      type:options.type
    })
    if(options.type=='collect'){
      this.setData({
        questions: app.globalData.collectList
      })
    }
    else{
      const db = wx.cloud.database()
      db.collection('questions').where({
        type:options.type
      }).get()
      .then(res=>{
        console.log(res);
        this.setData({
          questions:res.data,
          // customary:JSON.stringify(res.data)
        })
        // this.setData({
          // questions:JSON.parse(this.data.customary)
        // })
      }).catch(err=>{
        console.log('请求失败',err);
      })
    }
  },

  
})